package com.example.demo_text;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class ExamDAO {
    // Database connection string (using the same approach as other DAOs)
    private static final String JDBC_URL = databaseaccess.DatabaseUrl2;
    private static final String JDBC_USER = databaseaccess.DatabaseUser2;
    private static final String JDBC_PASSWORD =  databaseaccess.DatabasePassword2;

    // Get a database connection
    private static Connection getConnection() throws SQLException {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            return DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASSWORD);
        } catch (ClassNotFoundException e) {
            throw new SQLException("Database driver not found", e);
        }
    }

    // Close database resources
    private static void closeResources(Connection conn, Statement stmt, ResultSet rs) {
        try {
            if (rs != null) rs.close();
            if (stmt != null) stmt.close();
            if (conn != null) conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // Add a new exam
    public static boolean addExam(int courseId, String examTitle, Date examDate, Time examTime, 
                               int examDuration, String examDescription, boolean active) {
        Connection conn = null;
        PreparedStatement stmt = null;

        try {
            conn = getConnection();
            String sql = "INSERT INTO exams (course_id, exam_title, exam_date, exam_time, exam_duration, exam_description, active) " +
                         "VALUES (?, ?, ?, ?, ?, ?, ?)";
            
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, courseId);
            stmt.setString(2, examTitle);
            stmt.setDate(3, examDate);
            stmt.setTime(4, examTime);
            stmt.setInt(5, examDuration);
            stmt.setString(6, examDescription);
            stmt.setBoolean(7, active);
            
            return stmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            closeResources(conn, stmt, null);
        }
    }

    // Get all exams for a course
    public static List<Exam> getExamsByCourseId(String courseId) {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        List<Exam> exams = new ArrayList<>();

        try {
            conn = getConnection();
            String sql = "SELECT * FROM exams WHERE course_id = ? ORDER BY exam_date, exam_time";
            
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, Integer.parseInt(courseId));
            rs = stmt.executeQuery();
            
            while (rs.next()) {
                Exam exam = new Exam(
                    rs.getInt("exam_id"),
                    rs.getInt("course_id"),
                    rs.getString("exam_title"),
                    rs.getDate("exam_date"),
                    rs.getTime("exam_time"),
                    rs.getInt("exam_duration"),
                    rs.getString("exam_description"),
                    rs.getBoolean("active")
                );
                exams.add(exam);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeResources(conn, stmt, rs);
        }
        
        return exams;
    }

    // Get a specific exam by ID
    public static Exam getExamById(int examId) {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;

        try {
            conn = getConnection();
            String sql = "SELECT * FROM exams WHERE exam_id = ?";
            
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, examId);
            rs = stmt.executeQuery();
            
            if (rs.next()) {
                return new Exam(
                    rs.getInt("exam_id"),
                    rs.getInt("course_id"),
                    rs.getString("exam_title"),
                    rs.getDate("exam_date"),
                    rs.getTime("exam_time"),
                    rs.getInt("exam_duration"),
                    rs.getString("exam_description"),
                    rs.getBoolean("active")
                );
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeResources(conn, stmt, rs);
        }
        
        return null;
    }

    // Update exam status (activate/deactivate)
    public static boolean updateExamStatus(int examId, boolean active) {
        Connection conn = null;
        PreparedStatement stmt = null;

        try {
            conn = getConnection();
            String sql = "UPDATE exams SET active = ? WHERE exam_id = ?";
            
            stmt = conn.prepareStatement(sql);
            stmt.setBoolean(1, active);
            stmt.setInt(2, examId);
            
            return stmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            closeResources(conn, stmt, null);
        }
    }

    // Delete an exam
    public static boolean deleteExam(int examId) {
        Connection conn = null;
        PreparedStatement stmt = null;

        try {
            conn = getConnection();
            String sql = "DELETE FROM exams WHERE exam_id = ?";
            
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, examId);
            
            return stmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            closeResources(conn, stmt, null);
        }
    }
} 